package cn.open.feishu.alert.config;

import cn.open.feishu.alert.common.enums.UserModeEnum;
import cn.open.feishu.alert.context.SpringContext;
import cn.open.feishu.alert.support.FeishuAlertClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;
import java.util.Objects;

/**
 * @Description 飞书机器人配置类
 * @Author fandy.lin
 * @Date 2021/9/8 上午9:54
 **/
@Data
public class FeiShuProperties {
    @Value("${feishu.alert.customize.send.url:https://open.feishu.cn/open-apis/bot/v2/hook/}")
    private String customizeSendUrl;
    @Value("${feishu.alert.app.send.url:https://open.feishu.cn/open-apis/message/v4/batch_send/}")
    private String appSendUrl;
    @Value("${feishu.alert.access.token.url:https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal}")
    private String tokenRefreshUrl;
    @Value("${feishu.alert.user.gain.url:https://open.feishu.cn/open-apis/contact/v3/users/batch_get_id}")
    private String userGainUrl;
    @Value("${feishu.alert.app.id:#{null}}")
    private String appId;
    @Value("${feishu.alert.app.secret:#{null}}")
    private String appSecret;
    @Value("#{${feishu.alert.robot.config:null}}")
    private Map<String, String> robotConfig;
    @Value("${feishu.alert.mode:#{null}}")
    private String alertMode;
    @Value("${feishu.alert.await.seconds:60}")
    private Long awaitTime;
    @Value("${feishu.alert.is.enable:true}")
    private Boolean isEnable;
    @Value("${feishu.alert.async.execute.switch:true}")
    private Boolean asyncExecuteSwitch;
    @Value("${feishu.alert.async.executor.core.size:#{null}}")
    private Integer corePoolSize;
    @Value("${feishu.alert.async.executor.max.size:#{null}}")
    private Integer maxPoolSize;
    @Value("${feishu.alert.type.cache.init.size:100}")
    private Integer cacheInitSize;
    @Value("${feishu.alert.type.cache.max.size:1000}")
    private Integer cacheMaxSize;
    @Value("${feishu.alert.count.limit:100}")
    private Integer cacheCountLimit;
    private String ownerEmails;
    private String ownerMobiles;

    @Value("${feishu.alert.owner.emails:#{null}}")
    private void updateUserByEmail(String emails) {
        ownerEmails = emails;
        FeishuAlertClient alertClient = SpringContext.getBean(FeishuAlertClient.class);
        if (Objects.nonNull(alertClient)) {
            alertClient.refreshUser(emails, UserModeEnum.EMAILS);
        }
    }

    @Value("${feishu.alert.owner.mobiles:#{null}}")
    private void updateUserByMoblie(String mobiles) {
        ownerMobiles = mobiles;
        FeishuAlertClient alertClient = SpringContext.getBean(FeishuAlertClient.class);
        if (Objects.nonNull(alertClient)) {
            alertClient.refreshUser(mobiles, UserModeEnum.MOBILES);
        }
    }
}
